package org.wenhua.yuan.and.module_flutter

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import org.wenhua.yuan.R
import org.wenhua.yuan.and.module_flutter.flutterM1.bridge.JFlutterCacheManager
import org.wenhua.yuan.and.module_flutter.flutterM1.fragment.BaseFlutterFragment
import org.wenhua.yuan.and.util.ImmersiveStatusBar

class JFlutterActivity : AppCompatActivity() {

    private var flutterFragment: Fragment? = null
    private var moduleName:String? = JFlutterCacheManager.MODULE_NAME_DEFAULT
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersiveStatusBar.setStatusBar(this, true,
            resources.getColor(R.color.colorCMBCPrimaryLight), false)
        setContentView(R.layout.activity_j_flutter_activity)

        setFragmentShow()
    }

    private fun setFragmentShow() {
        val tag = "finance_fragment_show"
        flutterFragment = supportFragmentManager.findFragmentByTag(tag)
        if (flutterFragment == null) {
            flutterFragment = InnerFragment(moduleName!!)
        }
        if (flutterFragment!!.isAdded) {
            supportFragmentManager.beginTransaction().show(flutterFragment!!)
        } else {
            supportFragmentManager.beginTransaction().add(R.id.root_view, flutterFragment!!,tag).commit()
        }
    }

    class InnerFragment(private val moduleName:String) : BaseFlutterFragment(moduleName) {
        override fun onDestroy() {
            super.onDestroy()
            // 进行flutteryEngine的销毁
            JFlutterCacheManager.instance!!.clearCache(moduleName)
        }
    }
}
